/**
 * Created by Administrator on 2017/3/14.
 */
var Progress = require('../../../src/libs/progress');
var RealName = require('../../src/complex/realName');
var AccountMes = require('../complex/accountMes');
var FinanceRecord = require('../complex/financeRecord');
var Root = React.createClass({
    getInitialState: function () {
      return{
          realName:PageDM.realNameInfo.value,
          investRecord: PageDM.investRecord.value,
          accountInvest: PageDM.accountInvest.value
      }
    },
    componentDidMount: function(){
        UEventHub.emit("pageLoad","hello world!");

    },
    searchChange: function (e) {

    },
    updateRealNameInfoHandler: function (v) {
        this.setState({
            realName: v
        })
    },
    updateAccountInvestHandler: function (v) {
        this.setState({
            accountInvest: v
        })
    },
    updatePaginationHandler: function(pageVal){
        var _this = this;
        _this.refs.pagination.innerHTML = "";
        if(pageVal.totalPage > 1 && pageVal.totalPage <= 7){
            for(var i = 0; i <= pageVal.totalPage+1; i++){
                var a = document.createElement("a");
                a.setAttribute("href","javascript:;");
                if(i==0){
                    a.innerHTML = "&lt;";
                    if(pageVal.curPage > 1){
                        a.onclick = function(){
                            PageDM.pagination.setValue({
                                totalPage: pageVal.totalPage,	//总页数
                                pageSize: pageVal.pageSize,	//每页显示条数
                                curPage: pageVal.curPage-1	//当前页码
                            });
                            PageDM.pagination.onClick();
                        };
                    }else{
                        a.className = "pagination-disabled";
                    }
                }else if(i == pageVal.totalPage+1){
                    a.innerHTML = "&gt;";
                    if(pageVal.curPage < pageVal.totalPage){
                        a.onclick = function(){
                            PageDM.pagination.setValue({
                                totalPage: pageVal.totalPage,	//总页数
                                pageSize: pageVal.pageSize,	//每页显示条数
                                curPage: pageVal.curPage+1	//当前页码
                            });
                            PageDM.pagination.onClick();
                        };
                    }else{
                        a.className = "pagination-disabled";
                    }
                }else{
                    a.innerHTML = i;
                    (function(ii){
                        a.onclick = function(){
                            PageDM.pagination.setValue({
                                totalPage: pageVal.totalPage,	//总页数
                                pageSize: pageVal.pageSize,	//每页显示条数
                                curPage: ii	//当前页码
                            });
                            PageDM.pagination.onClick();
                        };
                    })(i);
                    if(i == pageVal.curPage){
                        a.className = "current";
                    }
                }
                _this.refs.pagination.appendChild(a);
            }
        }else if(pageVal.totalPage > 7){
            var dotIndex = 0;
            if(pageVal.curPage<=5 || (pageVal.totalPage - pageVal.curPage) <= 4 ){
                if((pageVal.curPage - 1) < (pageVal.totalPage - pageVal.curPage)){
                    dotIndex = 6;
                }else{
                    dotIndex = 2;
                }
            }
            for(var i = 0; i < 9; i++){
                var a = document.createElement("a");
                a.setAttribute("href","javascript:;");
                if(i==0){
                    a.innerHTML = "&lt;";
                    if(pageVal.curPage > 1){
                        a.onclick = function(){
                            PageDM.pagination.setValue({
                                totalPage: pageVal.totalPage,	//总页数
                                pageSize: pageVal.pageSize,	//每页显示条数
                                curPage: pageVal.curPage-1	//当前页码
                            });
                            PageDM.pagination.onClick();
                        };
                    }else{
                        a.className = "pagination-disabled";
                    }
                }else if(i == 8){
                    a.innerHTML = "&gt;";
                    if(pageVal.curPage < pageVal.totalPage){
                        a.onclick = function(){
                            PageDM.pagination.setValue({
                                totalPage: pageVal.totalPage,	//总页数
                                pageSize: pageVal.pageSize,	//每页显示条数
                                curPage: pageVal.curPage+1	//当前页码
                            });
                            PageDM.pagination.onClick();
                        };
                    }else{
                        a.className = "pagination-disabled";
                    }
                }else{
                    if(dotIndex == 0){
                        if(i == 2 || i == 6){
                            if(i == 2){
                                a.onclick = function(){
                                    PageDM.pagination.setValue({
                                        totalPage: pageVal.totalPage,	//总页数
                                        pageSize: pageVal.pageSize,	//每页显示条数
                                        curPage: pageVal.curPage-3	//当前页码
                                    });
                                    PageDM.pagination.onClick();
                                };
                                a.className = "jump-prev";
                                a.setAttribute("title","Previous 3 Pages");
                            }else if(i == 6){
                                a.onclick = function(){
                                    PageDM.pagination.setValue({
                                        totalPage: pageVal.totalPage,	//总页数
                                        pageSize: pageVal.pageSize,	//每页显示条数
                                        curPage: pageVal.curPage+3	//当前页码
                                    });
                                    PageDM.pagination.onClick();
                                };
                                a.className = "jump-next";
                                a.setAttribute("title","Next 3 Pages");
                            }
                        }else{
                            if(i == 1){
                                a.innerHTML = 1;
                                a.onclick = function(){
                                    PageDM.pagination.setValue({
                                        totalPage: pageVal.totalPage,	//总页数
                                        pageSize: pageVal.pageSize,	//每页显示条数
                                        curPage: 1	//当前页码
                                    });
                                    PageDM.pagination.onClick();
                                };
                            }else if(i == 7){
                                a.innerHTML = pageVal.totalPage;
                                a.onclick = function(){
                                    PageDM.pagination.setValue({
                                        totalPage: pageVal.totalPage,	//总页数
                                        pageSize: pageVal.pageSize,	//每页显示条数
                                        curPage: pageVal.totalPage	//当前页码
                                    });
                                    PageDM.pagination.onClick();
                                };
                            }else if(i == 3){
                                a.innerHTML = pageVal.curPage-1;
                                a.onclick = function(){
                                    PageDM.pagination.setValue({
                                        totalPage: pageVal.totalPage,	//总页数
                                        pageSize: pageVal.pageSize,	//每页显示条数
                                        curPage: pageVal.curPage-1	//当前页码
                                    });
                                    PageDM.pagination.onClick();
                                };
                            }else if(i == 4){
                                a.innerHTML = pageVal.curPage;
                                a.onclick = function(){
                                    PageDM.pagination.setValue({
                                        totalPage: pageVal.totalPage,	//总页数
                                        pageSize: pageVal.pageSize,	//每页显示条数
                                        curPage: pageVal.curPage	//当前页码
                                    });
                                    PageDM.pagination.onClick();
                                };
                                a.className = "current";
                            }else if(i == 5){
                                a.innerHTML = pageVal.curPage+1;
                                a.onclick = function(){
                                    PageDM.pagination.setValue({
                                        totalPage: pageVal.totalPage,	//总页数
                                        pageSize: pageVal.pageSize,	//每页显示条数
                                        curPage: pageVal.curPage+1	//当前页码
                                    });
                                    PageDM.pagination.onClick();
                                };
                            }
                        }
                    }else if(dotIndex == 2){
                        if(i == dotIndex){
                            a.onclick = function(){
                                PageDM.pagination.setValue({
                                    totalPage: pageVal.totalPage,	//总页数
                                    pageSize: pageVal.pageSize,	//每页显示条数
                                    curPage: pageVal.curPage-3	//当前页码
                                });
                                PageDM.pagination.onClick();
                            };
                            a.className = "jump-prev";
                            a.setAttribute("title","Previous 3 Pages");
                        }else{
                            if(i == 1){
                                a.innerHTML = 1;
                                a.onclick = function(){
                                    PageDM.pagination.setValue({
                                        totalPage: pageVal.totalPage,	//总页数
                                        pageSize: pageVal.pageSize,	//每页显示条数
                                        curPage: 1	//当前页码
                                    });
                                    PageDM.pagination.onClick();
                                };
                            }else{
                                a.innerHTML = (pageVal.totalPage-(7-i));
                                (function(i){
                                    a.onclick = function(){
                                        PageDM.pagination.setValue({
                                            totalPage: pageVal.totalPage,	//总页数
                                            pageSize: pageVal.pageSize,	//每页显示条数
                                            curPage: pageVal.totalPage-(7-i)	//当前页码
                                        });
                                        PageDM.pagination.onClick();
                                    };
                                })(i);
                            }
                        }
                        if((pageVal.totalPage-(7-i)) == pageVal.curPage){
                            a.className = "current";
                        }
                    }else if(dotIndex == 6){
                        if(i == dotIndex){
                            a.onclick = function(){
                                PageDM.pagination.setValue({
                                    totalPage: pageVal.totalPage,	//总页数
                                    pageSize: pageVal.pageSize,	//每页显示条数
                                    curPage: pageVal.curPage+3	//当前页码
                                });
                                PageDM.pagination.onClick();
                            };
                            a.className = "jump-next";
                            a.setAttribute("title","Next 3 Pages");
                        }else{
                            if(i == 7){
                                a.innerHTML = pageVal.totalPage;
                                a.onclick = function(){
                                    PageDM.pagination.setValue({
                                        totalPage: pageVal.totalPage,	//总页数
                                        pageSize: pageVal.pageSize,	//每页显示条数
                                        curPage: pageVal.totalPage	//当前页码
                                    });
                                    PageDM.pagination.onClick();
                                };
                            }else{
                                a.innerHTML = i;
                                (function(i){
                                    a.onclick = function(){
                                        PageDM.pagination.setValue({
                                            totalPage: pageVal.totalPage,	//总页数
                                            pageSize: pageVal.pageSize,	//每页显示条数
                                            curPage: i	//当前页码
                                        });
                                        PageDM.pagination.onClick();
                                    };
                                })(i);
                            }
                        }
                        if(i == pageVal.curPage){
                            a.className = "current";
                        }
                    }
                }
                _this.refs.pagination.appendChild(a);
            }
        }
    },
    updateInvestRecordHandler: function (v) {
        this.setState({
            investRecord:v
        })
    },
    render: function () {
        var _this = this;
        var PageDM = window.PageDM;
        var investRecordDom = [];
        // UEventHub.on(PageDM.pagination.dmbind + UI_EVENT_CONST.SET_UDM_VALUE_EVENT, function(v){
        //     _this.updatePaginationHandler(v);
        // });
        UEventHub.on(PageDM.investRecord.dmbind + UI_EVENT_CONST.SET_UDM_VALUE_EVENT, function(v){
            _this.updateInvestRecordHandler(v);
        });
        UEventHub.on(PageDM.accountInvest.dmbind + UI_EVENT_CONST.SET_UDM_VALUE_EVENT, function(v){
            _this.updateAccountInvestHandler(v);
        });
        //个人实名信息
        UEventHub.on(PageDM.realNameInfo.dmbind + UI_EVENT_CONST.SET_UDM_VALUE_EVENT, function(v){
            _this.updateRealNameInfoHandler(v);
        });
        // UEventHub.on(PageDM.stautsOption.dmbind + UI_EVENT_CONST.SET_UDM_VALUE_EVENT, function(v){
        //     _this.updateInvestRecordHandler(v);
        // });
        PageDM.pagination.setValue(PageDM.pagination.value);
        if(this.state.investRecord.length > 0){

            investRecordDom.push(<FinanceRecord data={this.state.investRecord} />)
        }else {
            investRecordDom.push(
                <div className="nodata">
                    <i className="bg-2"></i>您还没有在本平台成功融资过，如果有需要，现在前往 <a href={PageDM.investRecord.goFinanceUrl} className="fc-link"> 我要借款</a> 进入融资流程！
                </div>
               );
        }
        return (
            <div>
                <div className="header">
                    <div className="top-bar fc-light">
                        <div className="container">
                            <span>服务热线：0816-2373017</span>
                            <span>服务时间：工作日 9:00-18:00</span>
                            <span className="nav-right">
                            <a href="登录.html">登录</a>
                            <a href="注册.html">注册</a>
                            <a href="../帮助中心/平台介绍.html">帮助中心</a>
                            <a href="../index.html">返回绵阳商业银行</a>
                        </span>
                        </div>
                    </div>
                    <div className="header-nav">
                        <div className="container">
                            <div className="logo f-left">
                                <a href="../index.html">
                                    <img src="../images/logo.jpg" alt=""/>
                                </a>
                                <a href="../index.html">
                                    <img src="../images/logoe.jpg" alt=""/>
                                </a>
                            </div>
                            <div className="nav f-right">
                                <ul className="clearfix">
                                    <li><a href="../index.html">首页</a></li>
                                    <li><a href="../我要投资.html">我要投资</a></li>
                                    <li><a href="../我要借款.html">我要借款</a></li>
                                    <li><a href="../账户中心/我的账户.html">我的账户</a></li>
                                </ul>
                            </div>
                            <div className="clear"></div>
                        </div>
                        <div className="shadow"></div>
                    </div>
                </div>

                <div className="mod-account container">
                    <div className="sub-nav" ref="realName">
                        <RealName data={this.state.realName}/>
                    </div>
                    <div className="account my-account">
                        <div className="count" ref="accountInvest">
                            <AccountMes {...this.state.accountInvest}/>
                        </div>
                        <div className="investment-record financing ">
                            <div className="title"><b></b>最近待还项目<b></b></div>
                            <div  ref="investRecord">{investRecordDom}</div>
                        </div>
                    </div>
                    <div className="clear"></div>
                </div>
                <div className="pagination account"  ref="pagination"></div>

                <div className="footer">
                    <div className="container">
                        <div className="footer-logo f-left">
                            <p className="logo-foot"><img src="../images/foot-logo.png" alt=""/></p>
                            <p className="txt">
                                2005－2013 绵阳市商业银行股份有限公司
                                <a href="http://www.miitbeian.gov.cn" target="_blank">粤ICP备11013283123号</a>
                            </p>
                        </div>
                        <div className="footer-nav f-right">
                            <div className="col">
                                <h3>关于我们</h3>
                                <ul>
                                    <li><a href="../帮助中心/平台介绍.html">平台介绍</a></li>
                                    <li><a href="../帮助中心/名词解释.html">名词解释</a></li>
                                    <li><a href="../帮助中心/联系我们.html">联系我们</a></li>
                                    <li><a href="../帮助中心/服务协议.html">服务协议</a></li>
                                </ul>
                            </div>
                            <div className="col">
                                <h3>安全保障</h3>
                                <ul>
                                    <li><a href="../帮助中心/安全保障.html">项目安全</a></li>
                                    <li><a href="../帮助中心/安全保障.html">资金安全</a></li>
                                    <li><a href="../帮助中心/安全保障.html">信息安全</a></li>
                                    <li><a href="../帮助中心/安全保障.html">账户安全</a></li>
                                </ul>
                            </div>
                            <div className="col">
                                <h3>帮助中心</h3>
                                <ul>
                                    <li><a href="../帮助中心/投资操作指引.html">投资指引</a></li>
                                    <li><a href="../帮助中心/融资操作指引.html">融资指引</a></li>
                                    <li><a href="../帮助中心/平台公告.html">平台公告</a></li>
                                    <li><a href="javascript:;">常见问题</a></li>
                                </ul>
                            </div>
                            <div className="col col-service">
                                <p>客服专线<br/>0816-2373017</p>
                                <p>服务时间<br/>工作日9:00 - 18:00</p>
                            </div>
                        </div>
                        <div className="clear"></div>
                    </div>
                </div>
            </div>
        )
    }
});
ReactDOM.render(<Root/>, document.getElementById('main'));